﻿using Newtonsoft.Json;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.Common.C_Enum;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Serv;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using STHPMS1.Common.C_Extension;
using System.Linq;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/origpv_reg")]
    public class Dm_Origpv_RegPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("model/{model?}", Name = "Getorigpv_regByPage")]
		public IHttpActionResult Get(string model = null)
        {
            IModelCalculateService modelServ=this.Scene.Bll.Dm_Origpv_Reg.Load(model);
            var data = GetData(modelServ);
            if (data == null) return NotFound();
            return Ok(data);
        }


        private dynamic GetData(IModelCalculateService modelServ)
        {
            List<Object> series = new List<object>();
            List<double[]> data = new List<double[]>();
            string formula = "";
            string type = "";
            double yMax = 100;

            if (modelServ.Model.Formulatype == FormulaEnum.Equation1.GetEnumValue())
            {
                type = "line";
                double x  = 0;
                double y = 100;
                data.Add(new double[2] { x, y });

                y = 0;
                x = modelServ.CalculateX(y);
                data.Add(new double[2] { x, y });


                x = x/2;
                y = modelServ.CalculateY(x);
                data.Add(new double[2] { x, y });
            }

            if(modelServ.Model.Formulatype==FormulaEnum.Equation2.GetEnumValue())
            {
                type = "line";
                double x = 0;
                double y = 100;
                data.Add(new double[2] { x, y });

                y = 0;
                x = modelServ.CalculateX(y);
                data.Add(new double[2] { x, y });



                x = x/2;
                y = modelServ.CalculateY(x);
                data.Add(new double[2] { x, y });
            }

            if (modelServ.Model.Formulatype == FormulaEnum.Equation3.GetEnumValue())
            {
                type = "spline";
                for (int x = 0; x < 15; x++)
                {
                    double y = modelServ.CalculateY(x);
                    data.Add(new double[2] { x, y });
                }    
            }

            formula = modelServ.PrintForumla();

            series.Add(
                 new
                 {
                     type = "line",
                     name = modelServ.Model.Modelid,
                     marker = new { enabled = false },
                     states = new { hover = new { lineWidth = 0 } },
                     enableMouseTracking = false,
                     data = data.ToArray()
                 }
            );

            if (modelServ.Model.Formulatype == FormulaEnum.Equation1.GetEnumValue())
            {
                yMax = 0;
                dynamic modelValue = JsonConvert.DeserializeObject(modelServ.Model.Modelvalue);
                foreach (var jsonX in modelValue)
                {
                    List<double[]> yList = new List<double[]>();
                    foreach (var jsonY in jsonX.YList)
                    {
                        double[] d = new double[2];
                        d[0] = jsonX.X;
                        d[1] = jsonY.Y;
                        yList.Add(d);
                        yMax=d[1] > yMax ? d[1] : yMax;
                    }                  

                    series.Add(
                       new
                       {
                           type = "scatter",
                           name = string.Format("回归第{0}年值", jsonX.X),
                           marker = new { radius = 4 },
                           data = yList.ToArray()
                       }
                  );
                }
                yMax = Math.Ceiling(yMax);
            }

            var outData = new
            {
                credits = new {
                    enabled = false
                },
                title = new
                {
                    text = formula,
                    y=100
                },
                xAxis = new
                {
                    title=new
                    {
                        text = "路龄/年"
                    },
                    min = 0,
                    max = 15
                },
                yAxis = new
                {
                    title = new
                    {
                        text = ((RegressionIndicatorsEnum)modelServ.Model.Regre_Index).GetEnumName()
                    },
                    min = 0,
                    max = yMax
                },
                legend = new
                {
                    layout = "vertical",
                    align="right",
                    verticalAlign="top",
                    x=0,
                    y=0,
                    floating=true,
                    borderWidth=1
                },
                series = series.ToArray(),
                model=modelServ.Model
            };
            return outData;
        }

        [Route("")]
        public HttpResponseMessage Post(Dm_Origpv_Reg entity)
        {
            this.Scene.Bll.Dm_Origpv_Reg.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getorigpv_regByPage", new { model = entity.Modelid });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("")]
        public void Delete(Dm_Origpv_Reg entity)
        {
            this.Scene.Bll.Dm_Origpv_Reg.Remove(entity);
        }

        [Route("group")]
        public dynamic GetModelGroup()
        {
            return this.Scene.Bll.Dm_Oripv_Anagroup.Get<Dm_Oripv_Formula>(null, null);
        }

        [Route("models")]
        public dynamic GetModels()
        {
            var result=from p in this.Scene.Bll.Dm_Oripv_Anagroup.Get<Dm_Origpv_Reg>(null, null)
                       orderby p.Formulatype
                       select new {
                           Modelid=p.Modelid,
                           ModelGroup=FormatModelGroup((int)p.Formulatype)
                       };
            return result;
        }

        private string FormatModelGroup(int formulaType)
        {
            string result="";
            if(formulaType==FormulaEnum.Equation1.GetEnumValue())
            {
                result = "原路面直线形组";
            }
            else if (formulaType == FormulaEnum.Equation2.GetEnumValue())
            {
                result = "养护后直线形组";
            }
            else if (formulaType == FormulaEnum.Equation3.GetEnumValue())
            {
                result = "养护后多项式组";
            }

            return result;
        }
    }
}